﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using BindableDataGrid.Data;
using SilveR.GUIValidators;

namespace SilveR.Views.StatsViews
{
    public partial class PValueAdjustment : UserControl, IAnalysis
    {
        private PValueAdjustmentVariables pVariables;

        public PValueAdjustment()
        {
            InitializeComponent();

            pVariables = new PValueAdjustmentVariables();
            this.DataContext = pVariables;
        }

        #region IAnalysis Members

        public bool ValidateGUI()
        {
            List<ValidationResult> validationResult = new List<ValidationResult>();
            bool validatedOK = Validator.TryValidateObject(pVariables, new ValidationContext(pVariables, null, null), validationResult, true);

            if (!validatedOK)
            {
                ValidationMessagesWindow vmw = new ValidationMessagesWindow(validationResult);
                vmw.Show();
            }

            return validatedOK;
        }

        public event DataValidatedEventHandler DataValidated;

        public void ValidateData()
        {
            //no data validation required so raise event as true
            if (DataValidated != null)
                DataValidated(this, new DataValidatedEventArgs(true));
        }

        private void vmw_Closed(object sender, EventArgs e)
        {
            ValidationMessagesWindow vmw = (ValidationMessagesWindow)sender;

            if (DataValidated != null)
                DataValidated(this, new DataValidatedEventArgs(vmw.DialogResult.Value));
        }

        public string[] ExportData()
        {
            return null;
        }

        public DataHandler DataHandler
        {
            get { return null; }
        }

        public Dictionary<string, string> GetAnalysisArguments()
        {
            Dictionary<string, string> arguments = new Dictionary<string, string>();
            arguments.Add("P Values", pVariables.PValues); //4
            arguments.Add("Test Type", pVariables.SelectedTest); //5
            arguments.Add("Significance Level", pVariables.Significance); //6
            
            return arguments;
        }

        public string AnalysisName
        {
            get { return "P-value Adjustment"; }
        }

        public string ScriptName
        {
            get { return "PValueAdjustment.R"; }
        }

        public void UpdateDataHandlers()
        {
        }

        #endregion
    }

    public class PValueAdjustmentVariables : INotifyPropertyChanged
    {
        public PValueAdjustmentVariables()
        {
            //App app = (App)Application.Current;
            //availableDataHandlers = app.DataHandlers;
        }

        private DataHandler selectedDataHandler;
        public DataHandler SelectedDataHandler
        {
            get { return selectedDataHandler; }
            set
            {
                selectedDataHandler = value;
                //NotifyPropertyChanged("SelectedDataHandler");
            }
        }

        private ObservableCollection<DataHandler> availableDataHandlers = new ObservableCollection<DataHandler>();
        public ObservableCollection<DataHandler> AvailableDataHandlers
        {
            get
            {
                return availableDataHandlers;
            }

            set
            {
                availableDataHandlers = value;
                //NotifyPropertyChanged("AvailableDataHandlers");
            }
        }

        private string pValues;
        [CustomValidation(typeof(PValueAdjustmentGUIValidator), "CheckPValue")]
        public string PValues
        {
            get { return pValues; }
            set
            {
                pValues = value;
                //NotifyPropertyChanged("PValues");
            }
        }

        private string selectedTest;
        [CustomValidation(typeof(PValueAdjustmentGUIValidator), "CheckStringVarSet")]
        public string SelectedTest
        {
            get { return selectedTest; }
            set { selectedTest = value; }
        }

        private List<string> multipleComparisonTests = new List<string>() { "Holm", "Hochberg", "Hommel", "Benjamini-Hochberg", "Bonferroni" };
        public List<string> MultipleComparisonTests
        {
            get { return multipleComparisonTests; }
        }

        private string significance = "0.05";
        public string Significance
        {
            get { return significance; }
            set { significance = value; }
        }

        private List<string> significanceList = new List<string>() { "0.1", "0.05", "0.01", "0.001" };
        public List<string> SignificanceList
        {
            get { return significanceList; }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        // NotifyPropertyChanged will raise the PropertyChanged event, passing the source property that is being updated.
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}